import { _decorator, Component, EditBox, Node } from 'cc';
import { GameApp } from '../Game/GameApp';
import { ParamUI } from './ParamUI';
import UiManager from './UiManager';
import { plog } from '../Utils/Tools';
import NetEventMgr from '../Game/NetEventMgr';
import { MsgBox } from './MsgBox';
import msg from '../Msg/msgpb.js';
import { DataMgr } from '../Game/DataMgr';
const { ccclass, property } = _decorator;

@ccclass('UiLogin')
export class UiLogin extends Component {

    @property(Node)
    nodeBack: Node = null;
    @property(Node)
    btnNodeLogin: Node = null;
    @property(Node)
    btnNodeRegister: Node = null;
    @property(EditBox)
    ebAccount: EditBox = null;
    @property(EditBox)
    ebPassword: EditBox = null;

    public ResetParam(pm: any, type: number | undefined = undefined) {
        plog("UiLogin ResetParam pm:", pm)
    }


    onLoad(): void {
        // let pu = this.node.getComponent(ParamUI)
        // pu.RegisterCallbackOpen()
        // pu.RegisterCallbackOpen()
        // pu.SetCallbackRefresh((pm, type) => this.ResetParam(pm, type))

        //--------btns on------
        this.btnNodeLogin.on("click", this.onBtnLoginClick, this);
        this.btnNodeRegister.on("click", this.onBtnRegisterClick, this);
        this.node.on(Node.EventType.TOUCH_END, this.onOutClick, this);
        this.nodeBack.on(Node.EventType.TOUCH_END, this.onBackClick, this);

        let [acc, pwd] = DataMgr.Instance.GetAccountInfo()
        if (acc) {
            this.ebAccount.string = acc
            this.ebPassword.string = pwd
        }

    }
    onDestroy() {
        //--------btns off------
        this.btnNodeLogin.off("click", this.onBtnLoginClick, this);
        this.btnNodeRegister.off("click", this.onBtnRegisterClick, this);
        this.node.off(Node.EventType.TOUCH_END, this.onOutClick, this);
        this.nodeBack.off(Node.EventType.TOUCH_END, this.onBackClick, this);
    }


    onBackClick(evt) {
        evt.bubbles = false
    }
    onOutClick(evt) {
        evt.bubbles = false
        UiManager.Instance.RemoveUI(this.node, this.node.name)
    }
    // 按钮点击事件的回调函数
    onBtnLoginClick() {
        let str = ""
        if (this.ebAccount.string == "") {
            str += "账号 "
        }
        if (this.ebPassword.string == "") {
            str += "密码 "
        }
        if (str != "") {
            str += "不能为空"
            let pu = this.getComponent(ParamUI)
            pu.Hint(str)
            return
        }
        /**
        let nem = NetEventMgr.Instance
        nem.StartConnect(() => {
            plog(msg)
            plog(msg.ClientEnterR)
            const mr = msg.ClientEnterR
            const ma = msg.ClientEnterA
            let msgR = mr.create()
            msgR.Accout = this.ebAccount.string
            msgR.Password = this.ebPassword.string
            msgR.isLogin = true
            NetEventMgr.Instance.SendMessage(mr.name, ma.name, mr.encode(msgR).finish(), (data) => {
                let pu = this.getComponent(ParamUI)
                let a = ma.decode(data);
                if (a.Err != null) {
                    pu.Hint(a.Err.Result)
                } else {
                    pu.Hint("登录成功", () => {
                        UiManager.Instance.ChangeUI(this.node, "from login", 'register')
                    })
                }
            })
        })
        /**/
        // let um = UiManager.Instance
        // let cover = um.AddCover(this.node, 2)
        // const mr = msg.AccountLoginR
        // const ma = msg.AccountLoginA
        // let m = mr.create()
        // m.Accout = this.ebAccount.string
        // m.Password = this.ebPassword.string
        // NetEventMgr.Instance.SendMessage(mr.name, ma.name, mr.encode(m).finish(), (mc: proto.msg.MsgConfig, data) => {
        //     this.node.removeChild(cover)
        //     let pu = this.getComponent(ParamUI)
        //     if (mc.Err != null) {//系统错误
        //         pu.Hint(mc.Err.Result)
        //     } else {
        //         let a = ma.decode(data);
        //         if (a.Err != null) {
        //             pu.Hint(a.Err.Result)
        //         } else {
        //             pu.Hint("登录成功", () => {
        //                 UiManager.Instance.ChangeUI(this.node, "from login", 'register')

        //             })
        //         }
        //     }
        // })

        // plog("onBtnLoginClick")
        // // UiManager.Instance.ShowMsgBox(this.node, "登录？？")
        // let pu = this.node.getComponent(ParamUI)
        // pu.MsgBox("登录？？", (ok) => this.CheckMsgBox(ok))
    }
    onBtnRegisterClick() {
        // let pu = this.getComponent(ParamUI)
        // pu.MsgBox("是否切换注册", (ok) => {
        //     if (ok) {
        //         UiManager.Instance.ChangeUI(this.node, "from login", 'register')
        //     }
        // })
        UiManager.Instance.ChangeUI(this.node, "from login", 'register')
    }



    start() {

    }

    update(deltaTime: number) {

    }
}


